/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myheartsgame;

import java.net.*;
import java.io.*;
import java.util.ArrayList;
/**
 *
 * @author KaKa
 */
public class Client {
    
    int _iPort = 3200;     
    Socket _talkingSocket;

    //OutputStream va BufferedWriter de gui du lieu
    OutputStream os;
    BufferedWriter bw;

    // Thread de lang nghe cac message
    ClientListener _listeningProcess;

    Client(String ipServer) throws IOException {

        String namePlayer = GlobalClass.myName;
        try {
            // Tao socket de lang nghe ket noi ben server            
            Socket listener = new Socket("localhost", _iPort);                                    
            //Tao thread de lang nghe
            _listeningProcess = new ClientListener(listener, namePlayer);

            //Tao socket moi de gui du lieu
            _talkingSocket = new Socket("localhost", _iPort);
            os = _talkingSocket.getOutputStream();
            bw = new BufferedWriter(new OutputStreamWriter(os));

            InputStream is = listener.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            // Lay ket qua tra ve cua server
            String _result = br.readLine();
            if(_result.equals("@Full@")){
                _result = "Server is full";
                throw new IOException(_result);
            }            

            SendMessage(GlobalClass.myName + " đã kết nối.");

        } catch (IOException io) {            
            throw io;
        }
    }

    public void SendMessage(String message){
        try {            
            bw.write(message);
            bw.newLine();
            bw.flush();
        } catch (IOException io) {
        }
    }

}

class ClientListener implements Runnable {

    String _MyName;
    Socket _listeningSocket;
    Thread _thListening;
    public String _receivedMessage;
    
    // Kiem tra da chia bai chua de nhan bai,
    // khoi tao thi la chua chia bai
    boolean _isDealed = false;
    ArrayList<String> _cardsInfos = new ArrayList<String>();

    ClientListener(Socket listSocket, String name) {
        try {
            _MyName = name;
            _listeningSocket = listSocket;
            System.out.println("Listen to Server");
            _thListening = new Thread(this, _MyName);
            _thListening.start();

        } catch (Exception ex) {
            System.out.println("Listening error");
        }
    }

    public void run() {

        BufferedReader br = null;
        InputStream is;
        try {
            is = _listeningSocket.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));

            do {
                _receivedMessage = br.readLine();

                // Kiem tra xem co phai chia bai ko
                // cac chu cai dau la @CARDS@_
                // thong tin la bai la @CARDS@_[suit]_[number]
                String checkCard = _receivedMessage.substring(0, 8);          
                if (checkCard.equals("@CARDS@_")) {
                    // Neu dang chia bai
                    if(!_isDealed) {
                        String cardInfo = _receivedMessage.substring(8);                        
                        _cardsInfos.add(cardInfo);

                        if(_cardsInfos.size() == 13) {
                            MainGame.InitPlayerCards(_cardsInfos);
                            // Chia bai
                            GlobalClass.isDealing = true;
                            _isDealed = true;
                        }
                    }                    
                } else {

                    GlobalClass.chatBox += _receivedMessage + "\n";
                    MainGame.RefreshChatBox();
                }
                                
            } while (true);
            
        } catch (IOException io) {
            System.out.println(io.toString());
        }
        finally{
            
            try {
            br.close();
            }
            catch (IOException io){
                System.out.println(io.toString());
            }
        }
    }
}
